package com.choosefine.it.worker.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import com.choosefine.it.worker.model.vo.WorkerSalaryStatisticsVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.worker.service.WorkerSalaryMonthService;
import com.choosefine.it.worker.vo.WorkerSalaryDayInterfaceExplainVO;

/**
 * 工资月表
 * 
 * Author：liaozhanggen
 * Create Date：2017年2月27日
 * Version：v2.0
 */
@RestController
@RequestMapping("/worker/workerSalaryMonth")
@Api(value="工资月相关信息",description="廖仉根")
public class WorkerSalaryMonthController extends BaseController{
    @Autowired
    WorkerSalaryMonthService workerSalaryMonthService;
	
    
    
    /**
     * 11.28 dzq 根据关系查找用户某时间段内应收款，以收款，工总数 (班组下的工人总价钱)
     * @param
     * @return
     */
    @ApiOperation(value="某班组某工人的月薪资列表-分页")
    @RequestMapping(value="/detail/{relationId}",method=RequestMethod.GET)
    public String selectMonthByUserCode(
    		@RequestParam(value="teamId",defaultValue="0")Integer teamId,
    		@RequestParam(value="startTime",defaultValue="0")long startTime,
    		@RequestParam(value="endTime",defaultValue="0")long endTime)throws Exception{
    	if(null == teamId || teamId.intValue() <= 0){
    		return responseFail(CommonConstant.PARAMETER_ERROR);
    	}
    	if(!timeJudge(startTime, endTime)){
    		return responseFail(CommonConstant.PARAMETER_ERROR);
		}
    	Map<String, Object> map=new HashMap<String, Object>();
    	map.put("teamId", teamId);
    	map.put("startTime", startTime);
    	map.put("endTime", endTime);
    	Map<String, Object> result = workerSalaryMonthService.selectWorkerAmountSumByTeamId(map);
		
    	if (null == result) {	//默认值处理
			result = new HashMap<String, Object>();
		}
		if (result.get("payable") == null) {
			result.put("payable", 0);
		}
		if (result.get("paid") == null) {
			result.put("paid", 0);
		}
		if (result.get("workTimeSum") == null) {
			result.put("workTimeSum", 0);
		}
		return responseSuccess(result);
    }
    
    /**
     * 11.28 dzq 根据关系查找用户某时间段内应收款，以收款，工总数 (班组下的工人总价钱)
     * @param
     * @return
     */
    @ApiOperation(value="某班组资金统计:总工数总应付金额，以付金额")
    @ApiImplicitParams({
    	@ApiImplicitParam(
    			name="teamId",value="班组id",dataType="int",required=true,defaultValue="0",paramType = "query"),
    	@ApiImplicitParam(
    			name="startTime",value="开始时间",dataType="long",required=false,defaultValue="0",paramType = "query"),
    	@ApiImplicitParam(
    			name="endTime",value="结束时间",dataType="long",required=false,defaultValue="0",paramType = "query")
    })
    @ApiResponses({
		@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
		@ApiResponse(code = ResultCode.FAILED, message = CommonConstant.PARAMETER_ERROR) })
    @RequestMapping(value="/countTeam",method=RequestMethod.GET)
    public String selectCountTeamId(
    		@RequestParam(value="teamId",defaultValue="0")Integer teamId,
    		@RequestParam(value="userCode",required=false)String userCode,//添加具体的工人过虑条件2017-02-27
    		@RequestParam(value="startTime",defaultValue="0")long startTime,
    		@RequestParam(value="endTime",defaultValue="0")long endTime)throws Exception{
    	if(null == teamId || teamId.intValue() <= 0){
    		return responseFail(CommonConstant.PARAMETER_ERROR);
    	}
    	if(!timeJudge(startTime, endTime)){
    		return responseFail(CommonConstant.PARAMETER_ERROR);
		}
    	Map<String, Object> map=new HashMap<String, Object>();
    	map.put("teamId", teamId);
    	map.put("userCode", userCode);//添加具体的工人过虑条件2017-02-27
    	map.put("startTime", startTime);
    	map.put("endTime", endTime);
    	Map<String, Object> result = workerSalaryMonthService.selectWorkerAmountSumByTeamId(map);
		
    	if (null == result) {	//默认值处理
			result = new HashMap<String, Object>();
		}
		if (result.get("payable") == null) {
			result.put("payable", 0);
		}
	
		if (result.get("paid") == null) {
			result.put("paid", 0);	//TODO 已付的需要调用依照支付功能来
		}
		if (result.get("workTimeSum") == null) {
			result.put("workTimeSum", 0);
		}
		return responseSuccess(result);
    }
    
    
    /**
     * 11.25dzq 根据teamid 查找所有工人
     * @param teamId
     * @return
     * @throws ParseException 
     */
	@ApiOperation(value="根据teamid 查找所有工人(与班组全员报表关系)")
	@ApiImplicitParams({
		@ApiImplicitParam(
				name="teamId",value="班组id",dataType="int",required=true,defaultValue="0",paramType = "query"),
		@ApiImplicitParam(
				name="startTime",value="开始时间",dataType="long",required=false,defaultValue="0",paramType = "query"),
		@ApiImplicitParam(
				name="endTime",value="结束时间",dataType="long",required=false,defaultValue="0",paramType = "query"),
		@ApiImplicitParam(
				name="pageNum",value="开始页",dataType="int",required=false,defaultValue="0",paramType = "query"),
		@ApiImplicitParam(
				name="pageSize",value="班组id",dataType="int",required=false,defaultValue="0",paramType = "query")
	})
	@ApiResponses({
		@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
		@ApiResponse(code = ResultCode.FAILED, message = CommonConstant.PARAMETER_ERROR) })
	@RequestMapping(value="/team",method=RequestMethod.GET)
    public String selectByTeamId(
    		@RequestParam(name="teamId",defaultValue="0")int teamId,
    		@RequestParam(name="userCode",required=false)String userCode,
    		@RequestParam(name="startTime",defaultValue="0")long startTime,
    		@RequestParam(name="endTime",defaultValue="0")long endTime,
    		PageDTO pageDTO) throws ParseException {
		if (teamId <= 0) {// 判断班组id小于0，开始时间大于结束时间，开始时间小于0，则返回参数错误
			return responseFail(CommonConstant.TEAM_IS_NULL);
		}
		if(!timeJudge(startTime, endTime)){
			return responseFail(CommonConstant.PARAMETER_ERROR);
		}
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("teamId",teamId);
		map.put("userCode",userCode);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return responseSuccess(workerSalaryMonthService.selectMemberByTeamId(map,pageDTO));//存放数据集
    }
	
	
	/**
     * 11.26dzq 用于查出某条关系(班组与工人)在某段时间下的（出工总数，应付总数，已付总数）比如：工人个人报表
     * @param
     * @return
     */
	@RequestMapping(value="/countRelation",method=RequestMethod.GET)
    @ApiOperation(value="用于查出某条关系在某段时间下的出工总数，应付总数，已付总数(与工人个人报表关系)",response=WorkerSalaryDayInterfaceExplainVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(
				name="relationId",value="关系id",dataType="int",required=true,defaultValue="0",paramType = "query"),
		@ApiImplicitParam(
				name="startTime",value="开始时间",dataType="long",required=false,defaultValue="0",paramType = "query"),
		@ApiImplicitParam(
				name="endTime",value="结束时间",dataType="long",required=false,defaultValue="0",paramType = "query")
	})
	@ApiResponses({
		@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
		@ApiResponse(code = ResultCode.FAILED, message = CommonConstant.PARAMETER_ERROR) })
   public String selectCountUserByRelation(
    		@RequestParam(name="relationId",defaultValue="0")int relation,
    		@RequestParam(name="startTime",defaultValue="0")long startTime,
    		@RequestParam(name="endTime",defaultValue="0")long endTime){
		if (relation <= 0) {// 判断关系id小于0，开始时间大于结束时间，开始时间小于0，则返回参数错误
    		return responseFail(CommonConstant.PARAMETER_ERROR);
		}
		if(!timeJudge(startTime, endTime)){
			return responseFail(CommonConstant.PARAMETER_ERROR);
		}
		
    	Map<String, Object> map =new HashMap<String, Object>();
    	map.put("relationId", relation);
    	map.put("startTime", startTime);
    	map.put("endTime", endTime);
    	Map<String, Object> result;
    	
		result = workerSalaryMonthService.selectCountByRelation(map);

		if (null == result) { // 默认值处理
			result = new HashMap<String, Object>();
		}
		if (result.get("payable") == null) {
			result.put("payable", 0.00);
		}
		if (result.get("paid") == null) {
			result.put("paid", 0.00);
		}
		if (result.get("workTimeSum") == null) {
			result.put("workTimeSum", 0.00);
		}
    	return responseSuccess(result);//查看工、应收、应付总数
    }
    
	/**
	 *   @Description：
	 * 
	 * @Author：yuxin
	 * @Create Date：2017/2/17/0017 @param： @return：
	 */
	@ApiOperation(value = "项目管理-班组资金管理-工人工资管理-工人工资明细统计(新)", response = WorkerSalaryStatisticsVO.class)
	@RequestMapping(value = "/statisticalSalary/{userCode}/{workerItemsRelationId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "用户编码", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "workerItemsRelationId", value = "工人所在项目-班组关联表id", required = true, dataType = "Integer", paramType = "path") })
	public String workerSalaryStatistics(@PathVariable String userCode, @PathVariable Integer workerItemsRelationId) {
		return responseSuccess(workerSalaryMonthService.workerSalaryStatistics(userCode, workerItemsRelationId));
	}

}
